const express = require('express');
const app = express();
const server = require('http').Server(app);
const bodyParser = require("body-parser");
const {use} = require("express/lib/router");
const url = require("url");
const io = require('socket.io')(server, {cors: true});


//头像
let userAvatar;
//用户账号
let userAccount;

//已注册用户账号信息
let usersInfo = [];
//用户在线人数
let usersList = [];


server.listen('8888', function () {
    console.log('is running');
})

app.use(bodyParser.json());
app.use(express.static(__dirname));
app.get("/info", function (req, res) {
    console.log(123);
    res.end();
})
app.get('/index', function (req, res) {
    //设置允许跨域的域名，*代表允许任意域名跨域
    // res.header("Access-Control-Allow-Origin", "*");
    // //允许的header类型
    // res.header("Access-Control-Allow-Headers", "*");
    // // res.header("Access-Control-Allow-Credentials",true);
    // //跨域允许的请求方式
    // res.header("Access-Control-Allow-Methods", "*");
    // // res.header("Content-Type","application/json;charset=utf-8")
    // res.header("Content-Type", 'text/html;charset=utf-8');
    // console.log('index', userNickname);
    // res.sendFile(__dirname+'/index.html');
    res.json({
        userAvatar,
        userAccount
    })
})
// app.get('/index.css', function (req, res) {
//     res.sendFile(__dirname + '/index.css')
// });

app.get('/login', function (req, res) {

    res.sendFile(__dirname + '/login.html');
})

//登录
app.post('/login', function (req, res) {
    console.log('sasa', req.body)
    let {account, password} = req.body;
    userAccount = account;
    if (usersInfo.length === 0) {
        res.json({
            ok: -1,
            msg: 'fail'
        })
        return;
    }
    usersInfo.forEach((item) => {
        if (item.account === account) {
            if (item.password === password) {
                userAvatar = item.avatar;
                res.json({
                    ok: 1,
                    msg: 'success',
                    avatar: item.avatar,
                    account: account
                });
                return;
            } else {
                res.json({
                    ok: -1,
                    msg: 'fail'
                });
                return;
            }
        }
    });
    res.json({
        ok: -1,
        msg: 'fail'
    });
    return;
})
//注册
app.post('/register', (req, res) => {
    console.log('req.body', req.body);
    let account = req.body.account;
    let password = req.body.password;
    let avatar = url.parse(req.body.avatar).pathname;
    console.log(avatar)
    //判断是否有重复存在的用户
    //在已有的用户列表中查找(usersInfo)
    usersInfo.forEach((item) => {
        if (item.account === account) {
            res.json({
                ok: -1,
                message: 'fail'
            })
            return;
        }
    });
    usersInfo.push({
        account, password, avatar
    });
    console.log(usersInfo)
    res.json({
        ok: 1,
        message: 'success'
    });
})

io.on("connection", (socket) => {
    let io_account;
    let io_avatar;

    //登录
    socket.on('login', data => {
        //判断用户有没有已经登录
        console.log('用户登录')
        io_avatar = data.avatar;
        io_account = data.account
        //加入用户在线列表【数组】
        usersList.push({io_account, io_avatar});
        //广播给所有人
        console.log(io_account);
        io.emit('addUsr', {io_account, io_avatar, usersList});
    })

    //收到信息
    socket.on('sendMsg', function (data) {
        io_avatar = data.avatar;
        console.log(data, 'avatar', io_avatar)
        io.emit('msg', data);
    })

    // 用户离开
    socket.on('disconnect', () => {
        //将用户从用户列表中删除,根据nickname删除
        usersList.forEach((item, index) => {
            console.log(item, index);
            if (item.io_account === io_account) {
                usersList.splice(index, 1);
                return;
            }
        });
        io.emit('someoneLeave', {
            usersList,
            avatar: io_avatar,
            account:io_account
        });
    });


});